from hongruan.Address import Address
from hongruan.bank import Bank


class ATMInterface:
    """ATM界面类，提供用户交互功能"""

    def __init__(self, bank: Bank):
        self.bank = bank

    def display_menu(self):
        """显示菜单"""
        print("\n欢迎使用中国农业银行账户管理系统")
        print("=" * 30)
        print("1. 开户")
        print("2. 存钱")
        print("3. 取钱")
        print("4. 转账")
        print("5. 查询账户")
        print("0. 退出")
        print("=" * 30)

    def run(self):
        """运行ATM界面"""
        while True:
            self.display_menu()
            choice = input("请选择业务 (0-5): ")

            if choice == "0":
                print("感谢使用，再见！")
                break

            try:
                if choice == "1":
                    self.handle_open_account()
                elif choice == "2":
                    self.handle_deposit()
                elif choice == "3":
                    self.handle_withdraw()
                elif choice == "4":
                    self.handle_transfer()
                elif choice == "5":
                    self.handle_query()
                else:
                    print("无效选择，请重新输入")
            except ValueError as e:
                print(f"操作失败: {e}")

    def handle_open_account(self):
        """处理开户业务"""
        print("\n=== 开户 ===")
        account_type = int(input("请输入账户类型 (1-储蓄账户, 2-支票账户): "))
        name = input("请输入姓名: ")

        while True:
            try:
                password = int(input("请输入6位数字密码: "))
                if not (100000 <= password <= 999999):
                    print("密码必须是6位数字")
                    continue
                break
            except ValueError:
                print("请输入有效的数字密码")

        country = input("请输入国家: ")
        province = input("请输入省份: ")
        street = input("请输入街道: ")
        house_number = input("请输入门牌号: ")
        address = Address(country, province, street, house_number)

        initial_balance = int(input("请输入初始存款金额: "))

        result = self.bank.open_account(
            card_type=account_type,
            name=name,
            password=password,
            address=address,
            balance=initial_balance
        )

        if result == 1:
            print("开户成功！")
            # 显示分配的账号
            for acc_num, acc in self.bank.accounts.items():
                if acc.name == name and acc.balance == initial_balance:
                    print(f"您的账号是: {acc_num}")
                    break
        elif result == 2:
            print("账号已存在")
        elif result == 3:
            print("用户库已满，无法开户")

    def handle_deposit(self):
        """处理存钱业务"""
        print("\n=== 存钱 ===")
        account_number = input("请输入账号: ")
        amount = int(input("请输入存款金额: "))

        if self.bank.deposit(account_number, amount):
            print("存款成功！")
        else:
            print("账号不存在")

    def handle_withdraw(self):
        """处理取钱业务"""
        print("\n=== 取钱 ===")
        account_number = input("请输入账号: ")

        while True:
            try:
                password = int(input("请输入密码: "))
                break
            except ValueError:
                print("请输入有效的数字密码")

        amount = int(input("请输入取款金额: "))

        result = self.bank.withdraw(account_number, password, amount)

        if result == 0:
            print("取款成功！")
        elif result == 1:
            print("账号不存在")
        elif result == 2:
            print("密码错误")
        elif result == 3:
            print("余额不足")

    def handle_transfer(self):
        """处理转账业务"""
        print("\n=== 转账 ===")
        from_account = input("请输入转出账号: ")
        to_account = input("请输入转入账号: ")

        while True:
            try:
                password = int(input("请输入转出账号密码: "))
                break
            except ValueError:
                print("请输入有效的数字密码")

        amount = int(input("请输入转账金额: "))

        result = self.bank.transfer(from_account, to_account, password, amount)

        if result == 0:
            print("转账成功！")
        elif result == 1:
            print("账号不存在")
        elif result == 2:
            print("密码错误")
        elif result == 3:
            print("余额不足")

    def handle_query(self):
        """处理查询业务"""
        print("\n=== 查询账户 ===")
        account_number = input("请输入账号: ")

        while True:
            try:
                password = int(input("请输入密码: "))
                break
            except ValueError:
                print("请输入有效的数字密码")

        self.bank.query_account(account_number, password)


# 主程序入口


if __name__ == "__main__":
    # 创建银行实例
    bank = Bank("中国农业银行昌平沙河支行")

    # 创建ATM界面实例
    atm = ATMInterface(bank)

    # 运行ATM界面
    atm.run()